package com.gm.blog.controller;

import com.gm.blog.enums.ResultEnum;
import com.gm.blog.pojo.Admin;
import com.gm.blog.service.AdminService;
import com.gm.blog.utils.Result;
import com.gm.blog.utils.ShiroUtils;
import com.gm.blog.utils.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @RequestMapping(value="/login",method = RequestMethod.POST)
    public Result<Object> login(@RequestBody Admin admin){
        //controller层一般不写业务，但是controller层一定要写参数校验
        if(admin==null|| StringUtils.isBlank(admin.getUsername())||StringUtils.isBlank(admin.getPassword())){
            return new Result(ResultEnum.PARAMS_NULL.getCode(),"用户名或者密码错误");
        }
        Subject subject = SecurityUtils.getSubject();
        AuthenticationToken token=new UsernamePasswordToken(admin.getUsername(),admin.getPassword());
        try{
         subject.login(token);
        }catch(UnknownAccountException e){
            return new Result<>(ResultEnum.DATA_NOT_FOUND.getCode(),"用户名在后台找不到");
        }catch(IncorrectCredentialsException e){
            return new Result<>(ResultEnum.DATA_NOT_FOUND.getCode(),"密码在后台找不到");
        }
        //登录成功之后的处理
        Serializable sessionID=subject.getSession().getId();
        Map<String,Object> map=new HashMap<>(2);
        map.put("token",sessionID);
        return new Result(map);
    }

    /**
     * 获取当前登录用户信息
     * @return
     */
    @RequestMapping(value = "/info",method = RequestMethod.GET)
    public Result<Admin> getLoginInfo(){
        Admin loginAdmin= ShiroUtils.getLoginAdmin();
        loginAdmin.setPassword("");
        return new Result<>(loginAdmin);
    }
    /**
     * 查询管理员
     */
    @RequestMapping(value="/getAdmin",method = RequestMethod.GET)
    public Result<Object> getAdmin(){
        Admin admin = adminService.getAdmin();
        return new Result<>(admin);
    }

    /**
     * 更新个人信息
     * @return
     */
    @RequestMapping(value = "/updateInfo",method = RequestMethod.PUT)
    public Result<Object> updateInfo(@RequestBody Admin admin){
        adminService.updateInfo(admin);
        return new Result<>("更新成功");
    }
    /**
     * 更新密码
     */
    @RequestMapping(value = "/updatePassword",method = RequestMethod.PUT)
    public Result<Object> updatePassword(@RequestBody Admin admin){
        adminService.updatePassword(admin);
        return new Result<>("更新密码成功");
    }


}
